/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved
*
*  This file is part of the GroupMe! Project. You can utilize
*  software of the GroupMe! project under the terms of the 
*  GNU Lesser General Public License as published by the
*  Free Software Foundation in version 2.1.
* 
*  The GNU Lesser General Public License can be found at:
*  http://www.gnu.org/licenses/lgpl.html
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.export.rdf;

import java.util.Collection;

import javax.sql.DataSource;

import net.groupme.export.IGroupExport;
import net.groupme.export.Utils;
import net.groupme.export.constants.General;
import net.groupme.export.constants.GroupConstants;
import net.groupme.export.constants.ResourceConstants;
import net.groupme.export.constants.TagConstants;
import net.groupme.model.Group;
import net.groupme.model.Tag;

import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.vocabulary.DC;
import com.hp.hpl.jena.vocabulary.RDF;

/**
 * This class provides..
 *
 * @author Daniel Plappert
 * @version 0.1
 */
public class GroupExport implements IRdfDataExport, IGroupExport {
    

    private Collection<Group> data = null;
    private DataSource        ds   = null;
    
    /**
     * Creates a new instance of GroupExport
     */
    private GroupExport() {}
    
    
    /**
     * 
     * @param DataSource 
     */
    public GroupExport(DataSource ds) {
        this.ds = ds;
    }
    
    
    /**
     *
     * @param Collection<Group>
     */
    public void setData(Collection<Group> data) {
        this.data = data;
    }
    
    
    /**
     * This method creates the rdf data for all groups
     * set by the setData method.
     *
     * @param Model 
     */
    public void exportData(Model model, boolean exportAll) {
        
        if ( data == null )
            return;
        
        Resource tempRes = null;
        Literal tempLit = null;
        //TagExport exportTag = new TagExport(ds);
        ResourceExport exportResources = new ResourceExport(ds);
        
        for(Group tempGroup : data) {
            
            // creating rdf resource description
            tempRes = model.createResource(tempGroup.getURI().toString());
            tempRes.addProperty(RDF.type, model.createResource(General.groupBaseUri));
            
            // name attribute
            tempLit = model.createTypedLiteral(tempGroup.getName());
            tempRes.addProperty(DC.title, tempLit);
            
            // description attribute
            tempLit = model.createTypedLiteral(tempGroup.getDescription());
            tempRes.addProperty(DC.description, tempLit);
            
            // url attribute
            tempLit = model.createTypedLiteral( (tempGroup.getURL() == null ? "" : tempGroup.getURL().toString()) );
            tempRes.addProperty(ResourceConstants.resourceURLProperty, tempLit);
        
            if(exportAll){
//            	export containing resources
            	exportResourceRelation(model, tempRes, tempGroup.getResources());
            	exportResources.setData(tempGroup.getResources());
            	exportResources.exportData(model, false);
            
            	// export all tags related to this group
            	exportTagAssignments(tempGroup, tempRes, model);
            	
            	//old:
            	/*exportTagRelation(model, tempRes, tempGroup.getTags());
            	exportTag.setData(tempGroup.getTags());
            	exportTag.exportData(model, true);*/
            }
        }
              
        
    }
    
    /**
     * Export TagAssignments to the given Jena Model.
     * @param resource the resource that is tagged
     * @param model the Jena Model that should be filled
     */
    private void exportTagAssignments(net.groupme.model.Resource resource, com.hp.hpl.jena.rdf.model.Resource asJenaResource, Model model){
    	for(TaggingContext tempTaggingContext : Utils.getTaggingContextByResourceId(ds,resource.getId())) {

             com.hp.hpl.jena.rdf.model.Resource blankNode = model.createResource();
             blankNode.addProperty(RDF.type, model.createResource(TagConstants.taggingContextUri));
             if(tempTaggingContext.getGroup() != null){//if groups are tagged directly then tempTaggingContext.getGroup() is null
            	 blankNode.addProperty(TagConstants.groupProperty, model.createResource(tempTaggingContext.getGroup().toString()));
             }
             blankNode.addProperty(TagConstants.tagProperty, model.createResource(tempTaggingContext.getUser().toString()));
             blankNode.addProperty(TagConstants.userProperty, model.createResource(tempTaggingContext.getUser().toString()));
             blankNode.addProperty(TagConstants.resourceProperty, model.createResource(tempTaggingContext.getResource().toString()));
             blankNode.addProperty(TagConstants.importanceProperty, model.createTypedLiteral(tempTaggingContext.getImportance()));
             blankNode.addProperty(TagConstants.creationDateProperty, model.createTypedLiteral(tempTaggingContext.getCreationDate(), XSDDatatype.XSDdate));

             asJenaResource.addProperty(ResourceConstants.hasTagProperty, blankNode);
         }
    }

    /**
     * 
     *
     * @param Model 
     * @param Resource
     * @param Collection<Tag>
     */
    private void exportTagRelation(Model model, com.hp.hpl.jena.rdf.model.Resource res, Collection<Tag> tagCollection) {

        for(Tag tag : tagCollection) {
            res.addProperty(ResourceConstants.hasTagProperty, 
                model.createResource(tag.getURI().toString()));            
        }
        
    }    
    
    
    /**
     * 
     *
     * @param Model
     * @param Resource
     * @param Collection<Resource>
     */
    private void exportResourceRelation(Model model, com.hp.hpl.jena.rdf.model.Resource res, Collection<net.groupme.model.Resource> resourceCollection) {
        
        for(net.groupme.model.Resource resource : resourceCollection) {
            res.addProperty(GroupConstants.hasResourceProperty, 
                model.createResource(resource.getURI().toString()));
        }
        
        
    } 

    
}
